/**
 * @description order router
 */
const router = require('koa-router')()
const { createOrder, getOrderList } = require('../controller/order')
const loginCheck = require('../middleware/loginCheck')
const { ErrorModel, SuccessModel } = require('../res-model/index')
router.prefix('/api/order')
//创建订单
router.post('/', loginCheck, async (ctx, next) => {
  const { username } = ctx.session.userInfo
  //订单数据
  const data = ctx.request.body
  try {
    const newOrder = await createOrder(username, data)
    ctx.body = new SuccessModel(newOrder)
  } catch (e) {
    console.error(e);
    ctx.body = new ErrorModel(-5, '订单创建失败')
  }
})
//获取订单列表
router.get('/', loginCheck, async (ctx, next) => {
  const {username}=ctx.session.userInfo
  const list=await getOrderList(username)
  ctx.body=new SuccessModel(list)
})
module.exports = router